Mapping

Here is an interactive map for the state of GA

knitr::opts_chunk$set(echo = TRUE)

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.1     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tmap)
library(tmaptools)
library(htmltools)
library(janitor)

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(rmapshaper)
library(here)
here() starts at /cloud/project
options(tigris_class = "sf")
knitr::opts_chunk$set(echo = TRUE)






library(tidyverse)
library(tigris)
library(sf)
library(tidycensus)
library(htmltools)
library(janitor)
library(here)
library(mapview)
library(leafsync)
library(leaflet.extras2)
Loading required package: leaflet
library(viridis)
Loading required package: viridisLite
options(tigris_class = "sf")
# uncomment to run, then recomment it out so you don't run it every time

#  census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", overwrite =TRUE)
 install.packages('leaflet.extras2')
Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
(as 'lib' is unspecified)
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)
#pull for GA counties
ga_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "GA",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
ga_counties_withgeo
Simple feature collection with 159 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -85.60516 ymin: 30.35785 xmax: -80.84038 ymax: 35.00124
Geodetic CRS:  NAD83
First 10 features:
   GEOID                      NAME totalpopE totalpopM medincomeE medincomeM
1  13021      Bibb County, Georgia    156711        NA      43862       1778
2  13049  Charlton County, Georgia     12416        NA      45494       5791
3  13283  Treutlen County, Georgia      6410        NA      35441       9710
4  13309   Wheeler County, Georgia      7568        NA      26776       3605
5  13279    Toombs County, Georgia     26956        NA      42975       3095
6  13077    Coweta County, Georgia    144928        NA      83486       2974
7  13153   Houston County, Georgia    161177        NA      70313       3057
8  13183      Long County, Georgia     16398        NA      52742       8858
9  13163 Jefferson County, Georgia     15708        NA      42238       4150
10 13261    Sumter County, Georgia     29690        NA      36687       2163
   medageE medageM                       geometry
1     36.2     0.3 MULTIPOLYGON (((-83.89192 3...
2     40.6     1.5 MULTIPOLYGON (((-82.4156 31...
3     39.9     5.3 MULTIPOLYGON (((-82.74762 3...
4     33.6    10.0 MULTIPOLYGON (((-82.93976 3...
5     37.8     0.9 MULTIPOLYGON (((-82.48038 3...
6     38.9     0.3 MULTIPOLYGON (((-85.0132 33...
7     35.9     0.3 MULTIPOLYGON (((-83.85685 3...
8     33.7     0.8 MULTIPOLYGON (((-81.98162 3...
9     40.5     0.8 MULTIPOLYGON (((-82.66192 3...
10    37.0     1.1 MULTIPOLYGON (((-84.44381 3...
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS:  NAD83
First 10 features:
   GEOID                              NAME totalpopE totalpopM medincomeE
1  20161              Riley County, Kansas     72602        NA      53296
2  19159             Ringgold County, Iowa      4739        NA      57700
3  30009            Carbon County, Montana     10488        NA      63178
4  16007           Bear Lake County, Idaho      6327        NA      60337
5  55011         Buffalo County, Wisconsin     13314        NA      61167
6  31185             York County, Nebraska     14164        NA      66337
7  08037            Eagle County, Colorado     55693        NA      91338
8  42129 Westmoreland County, Pennsylvania    355107        NA      64708
9  40079         Le Flore County, Oklahoma     48436        NA      43049
10 48053              Burnet County, Texas     48424        NA      65363
   medincomeM medageE medageM                       geometry
1        2489    25.5     0.1 MULTIPOLYGON (((-96.96095 3...
2        5058    44.3     1.0 MULTIPOLYGON (((-94.47167 4...
3        4261    50.7     0.9 MULTIPOLYGON (((-109.7987 4...
4        7039    38.9     1.1 MULTIPOLYGON (((-111.6345 4...
5        2352    46.5     0.5 MULTIPOLYGON (((-92.08384 4...
6        4128    39.5     1.2 MULTIPOLYGON (((-97.82629 4...
7        4058    37.8     0.8 MULTIPOLYGON (((-107.1137 3...
8        1350    47.1     0.2 MULTIPOLYGON (((-79.90487 4...
9        1869    38.8     0.3 MULTIPOLYGON (((-95.05996 3...
10       4694    44.7     0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"
ga_counties_withgeo <- ga_counties_withgeo %>%
  select(-ends_with("M"))

ga_counties_withgeo
Simple feature collection with 159 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -85.60516 ymin: 30.35785 xmax: -80.84038 ymax: 35.00124
Geodetic CRS:  NAD83
First 10 features:
   GEOID                      NAME totalpopE medincomeE medageE
1  13021      Bibb County, Georgia    156711      43862    36.2
2  13049  Charlton County, Georgia     12416      45494    40.6
3  13283  Treutlen County, Georgia      6410      35441    39.9
4  13309   Wheeler County, Georgia      7568      26776    33.6
5  13279    Toombs County, Georgia     26956      42975    37.8
6  13077    Coweta County, Georgia    144928      83486    38.9
7  13153   Houston County, Georgia    161177      70313    35.9
8  13183      Long County, Georgia     16398      52742    33.7
9  13163 Jefferson County, Georgia     15708      42238    40.5
10 13261    Sumter County, Georgia     29690      36687    37.0
                         geometry
1  MULTIPOLYGON (((-83.89192 3...
2  MULTIPOLYGON (((-82.4156 31...
3  MULTIPOLYGON (((-82.74762 3...
4  MULTIPOLYGON (((-82.93976 3...
5  MULTIPOLYGON (((-82.48038 3...
6  MULTIPOLYGON (((-85.0132 33...
7  MULTIPOLYGON (((-83.85685 3...
8  MULTIPOLYGON (((-81.98162 3...
9  MULTIPOLYGON (((-82.66192 3...
10 MULTIPOLYGON (((-84.44381 3...
#remove that trailing "E" for the state of GA

colnames(ga_counties_withgeo) <- sub("E$", "", colnames(ga_counties_withgeo)) # $ means end of string only

ga_counties_withgeo
Simple feature collection with 159 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -85.60516 ymin: 30.35785 xmax: -80.84038 ymax: 35.00124
Geodetic CRS:  NAD83
First 10 features:
   GEOID                       NAM totalpop medincome medage
1  13021      Bibb County, Georgia   156711     43862   36.2
2  13049  Charlton County, Georgia    12416     45494   40.6
3  13283  Treutlen County, Georgia     6410     35441   39.9
4  13309   Wheeler County, Georgia     7568     26776   33.6
5  13279    Toombs County, Georgia    26956     42975   37.8
6  13077    Coweta County, Georgia   144928     83486   38.9
7  13153   Houston County, Georgia   161177     70313   35.9
8  13183      Long County, Georgia    16398     52742   33.7
9  13163 Jefferson County, Georgia    15708     42238   40.5
10 13261    Sumter County, Georgia    29690     36687   37.0
                         geometry
1  MULTIPOLYGON (((-83.89192 3...
2  MULTIPOLYGON (((-82.4156 31...
3  MULTIPOLYGON (((-82.74762 3...
4  MULTIPOLYGON (((-82.93976 3...
5  MULTIPOLYGON (((-82.48038 3...
6  MULTIPOLYGON (((-85.0132 33...
7  MULTIPOLYGON (((-83.85685 3...
8  MULTIPOLYGON (((-81.98162 3...
9  MULTIPOLYGON (((-82.66192 3...
10 MULTIPOLYGON (((-84.44381 3...
#Here is our initial mapview for the state of GA
mapview(ga_counties_withgeo, zcol = "medincome")
# This set of code allows us to customize maps with colors. 

mapview(ga_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (159)! 
Interpolating color vector to match number of zcol values.
mapviewOptions("basemaps.color.shuffle" = FALSE)
mapview(ga_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (159)! 
Interpolating color vector to match number of zcol values.
map_income <- mapview(ga_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (159)! 
Interpolating color vector to match number of zcol values.
map_age <- mapview(ga_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (97)! 
Interpolating color vector to match number of zcol values.
#This line of code allows us to sync the maps together

sync(map_income, map_age)
#This is a side by side comparison from the leaflet.extras 2 package

map_income | map_age
mapview(ga_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (159)! 
Interpolating color vector to match number of zcol values.
#Here I am customizing my labels. 

mylabel <- glue::glue("{ga_counties_withgeo$NAM} {ga_counties_withgeo$medincome}")


mapview(ga_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (159)! 
Interpolating color vector to match number of zcol values.
mypopup <- glue::glue("<strong>{ga_counties_withgeo$NAM}</strong><br />
                      Total Population: {ga_counties_withgeo$totalpop}<br />
                      Median Income: {ga_counties_withgeo$medincome}") %>% 
  lapply(htmltools::HTML)

# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
#   lapply(htmltools::HTML)